package org.easy4j.framework.core.plugin;

import org.easy4j.framework.core.config.Config;

import java.util.*;

/**
 * @author bjliuyong
 * @version 1.0
 * @created date 16-8-1
 */
public final class ScanPackageHelper {

    /*********************************************************
     ** base.package
     ** app.package
     ** mvc.package
     **
     **
     *********************************************************
     */
    public static final String BASE_PACKAGE     = "base.package" ;

    public static final String BASE_APP_PACKAGE = "app.package" ;

    public static final String BASE_MVC_PACKAGE = "mvc.package" ;

    static List<String> basePackages = new ArrayList<String>() ;

    static List<String> mvcPackages  = new ArrayList<String>() ;

    static {

        ServiceLoader<ScanPackage> packages = ServiceLoader.load(ScanPackage.class);
        Iterator<ScanPackage> iterator = packages.iterator();
        for( ; iterator.hasNext() ; ) {
            ScanPackage pkg = iterator.next() ;
            pkg.registerBasePackages();
            pkg.registerMvcPackages();
        }

        DefaultScanPackage defaultScanPackage = new DefaultScanPackage();
        defaultScanPackage.registerBasePackages();
        defaultScanPackage.registerMvcPackages();
    }

    public static void registerBasePackage(String ... packages){

        if(packages == null || packages.length == 0)
            return ;
        for(String pkg : packages) {
            basePackages.add(pkg) ;
        }
    }

    public static void registerMvcPackage(String ... packages ) {

        if(packages == null || packages.length == 0)
            return ;

        for(String pkg : packages) {
            mvcPackages.add(pkg) ;
        }
    }

    public static String[] getBasePackages() {

        int len = basePackages.size() ;
        String[] arr = new String[len];

        for(int i = 0 ; i < len ; i++ ) {
            arr[i] = basePackages.get(i);
        }

        return arr ;
    }

    public static String[] getMvcPackages() {

        int len = mvcPackages.size() ;
        String[] arr = new String[len];

        for(int i = 0 ; i < len ; i++ ) {
            arr[i] = mvcPackages.get(i);
        }

        return arr ;
    }

    static class DefaultScanPackage implements  ScanPackage {

        /**
         * 获取Spring Root ApplicationContext 扫描的基本包
         *
         * @return
         */
        @Override
        public void registerBasePackages() {

            String prefix = Config.getValue(BASE_PACKAGE);
            registerBasePackage(prefix + ".service") ;
            registerBasePackage(prefix + ".dao") ;
            registerBasePackage(prefix + ".manager") ;
            registerBasePackage(prefix + ".interceptor") ;

            registerBasePackage("org.easy4j.framework.web.bean.processor");

            String rootPkg = Config.getValue(BASE_APP_PACKAGE);
            if(rootPkg != null ) {

                for(String pkg : rootPkg.split(",")) {
                    registerBasePackage(pkg);
                }
            }

        }

        /**
         * 获取Spring WebApplicationContext 扫描的基本包
         *
         * @return
         */
        @Override
        public void registerMvcPackages() {

            String prefix = Config.getValue(BASE_PACKAGE);
            registerMvcPackage(prefix + ".controller") ;

            String mvcPkg = Config.getValue(BASE_MVC_PACKAGE);
            if(mvcPkg != null ) {

                for(String pkg : mvcPkg.split(",")) {
                    registerMvcPackage(pkg);
                }
            }
        }
    }
}
